//
//  DDAddressVC.m
//  dingdang
//
//  Created by 阮 沧晖 on 15/8/1.
//  Copyright (c) 2015年 阮 沧晖. All rights reserved.
//

#import "DDAddressVC.h"
#import "DDNetWorkAPIClient.h"
#import "DDAddress.h"
#import "DDAddressAddInfoVC.h"

@interface DDAddressVC ()

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic,strong)NSArray *datas;
@end

@implementation DDAddressVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self createUI];
    [self createData];
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    __weak DDAddressVC * weakSelf = self;
    DDNetWorkAPIClient *network = [[DDNetWorkAPIClient alloc]init];
    [network queryAddress:^(BOOL isSuccess, id address, NSString *message, NSString *status) {
        DDAddress *add = (DDAddress *)address;
        _datas = add.data;
        [weakSelf.tableView reloadData];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)createUI
{
    __weak DDAddressVC *weakSelf = self;
    self.view.backgroundColor = RGB(245, 245, 245);
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBarHidden = YES;
    //自定义TAB BAR UI
    [self addNavigatorTitle:@"地址管理" parent:self.view];
    self.barViewControl.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"top_Background"]];
    [self AddLeftBtnAction:nil normal:@"top_返回" selected:@"top_返回" action:^{
        //[self dismissViewControllerAnimated:YES completion:nil];
        [self.navigationController popViewControllerAnimated:YES];
    }];
    [self AddRightBtnAction:@"新增地址" normal:nil selected:nil action:^{
        DDAddressAddInfoVC *addAddress = [[DDAddressAddInfoVC alloc] initWithNibName:@"DDAddressAddInfoVC" bundle:nil];
        addAddress.title = @"新增地址";
        [self.navigationController pushViewController:addAddress animated:YES];
    }];
}

- (void)createData
{
   
}


#pragma mark -UITableDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _datas.count;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 64;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"UITableViewCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    for (UIView *view in cell.contentView.subviews) {
        [view removeFromSuperview];
    }
    DDAddressCell *addsCell = _datas[indexPath.row];

    UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, self.tableView.frame.size.width/2, 24)];
    nameLabel.text = addsCell.consignee;
    [cell.contentView addSubview:nameLabel];
    
    UIButton *checkBox = [UIButton buttonWithType:UIButtonTypeCustom];
    checkBox.frame = CGRectMake(self.tableView.frame.size.width-21, 20, 16, 16);
    [checkBox setImage:[UIImage imageNamed:@"checkBox_off"] forState:UIControlStateNormal];
    [checkBox setImage:[UIImage imageNamed:@"checkBox_on"] forState:UIControlStateSelected];
    if ([addsCell.def isEqualToString:@"1"]) {
        checkBox.selected = YES;
    }
    [cell.contentView addSubview:checkBox];
    
    UILabel *phoneLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.tableView.frame.size.width/2, 5, self.tableView.frame.size.width/2-40, 24)];
    phoneLabel.text = addsCell.phoneNumber;
    phoneLabel.textAlignment = NSTextAlignmentRight;
    [cell.contentView addSubview:phoneLabel];
    
    UILabel *addressLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, [self yPostiong:phoneLabel sp:5], self.tableView.frame.size.width-40, 24)];
    addressLabel.text = addsCell.address;
    [cell.contentView addSubview:addressLabel];
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
 
    __weak DDAddressVC *weakSelf = self;
    if ([_source isEqualToString:@"exchange"]) {
        if (_addressResult) {
            _addressResult(weakSelf.datas[indexPath.row]);
            [weakSelf.navigationController popViewControllerAnimated:YES];
            
        }
    }else{
        DDAddressAddInfoVC *addInfoVC = [[DDAddressAddInfoVC alloc] initWithNibName:@"DDAddressAddInfoVC" bundle:nil];
         DDAddressCell *addsCell = _datas[indexPath.row];
        NSArray *address = @[addsCell.consignee,addsCell.phoneNumber,addsCell.postCode,addsCell.district,addsCell.address,addsCell.def];
        addInfoVC.addressID = addsCell.id;
        addInfoVC.upadtes = address;
        addInfoVC.title = @"修改地址";
        [self.navigationController pushViewController:addInfoVC animated:YES];
    }
}

-(float) xPostiong:(UIView *)view sp:(float)sp
{
    return view.frame.size.width+view.frame.origin.x + sp;
}

-(float) yPostiong:(UIView *)view sp:(float)sp
{
    return view.frame.size.height+view.frame.origin.y + sp;
}
@end
